(function() {
	var rechargePage = {
		init:function() {
			this.id_card = null;
			this.gate_id = null;
			this.eventCenter();
			this.getPlatFund();
			this.getValidateInfo();

        },
		eventCenter: function() {
			var me = this;
			$('.one_block').hover(function() {
				$(this).find('.ob_icon').addClass('hover');
			},function() {
				$(this).find('.ob_icon').removeClass('hover');
			});
			$("#buy_amount").keypress(function(event) {
				var keyCode = event.which;
				if ((keyCode >= 48 && keyCode <=57))
					return true;
				else
					return false;
			}).focus(function() {
				this.style.imeMode='disabled';
			});
            $('.recharge_type li a').click(function(event) {
                if(event.preventDefault){
                    event.preventDefault();
                }else{
                    event.returnValue = false;
                }
                $('.recharge_type li').removeClass('active');
                $(event.target.parentElement).addClass('active');
                $('.bank_list').empty();
                $('.bank_list a').unbind('click');
                if($(event.target).attr('id') == 'more_tab') {
                    $('.bank_list').html(
                        '<a id="g9999" href="#"><img width="235" height="60" src="/static/home/lib/img/bank/chinapnr.jpg"></a>'
                    );
                } else {
                    $('.bank_list').html(
                        '<a id="g25" href="#"><img src="/static/home/lib/img/bank/Bank01.jpg"></a>'+
                        '<a id="g29" href="#"><img src="/static/home/lib/img/bank/Bank02.jpg"></a>'+
                        '<a id="g45" href="#"><img src="/static/home/lib/img/bank/Bank03.jpg"></a>'+
                        '<a id="g27" href="#"><img src="/static/home/lib/img/bank/Bank04.jpg"></a>'+
                        '<a id="g46" href="#"><img src="/static/home/lib/img/bank/Bank05.jpg"></a>'+
                        '<a id="g21" href="#"><img src="/static/home/lib/img/bank/Bank06.jpg"></a>'+
                        '<a id="g28" href="#"><img src="/static/home/lib/img/bank/Bank07.jpg"></a>'+
                        '<a id="g16" href="#"><img src="/static/home/lib/img/bank/Bank08.jpg"></a>'+
                        '<a id="g09" href="#"><img src="/static/home/lib/img/bank/Bank09.jpg"></a>'+
                        '<a id="g13" href="#"><img src="/static/home/lib/img/bank/Bank10.jpg"></a>'+
                        '<a id="g19" href="#"><img src="/static/home/lib/img/bank/Bank11.jpg"></a>'+
                        '<a id="more_bank" href="#"><img src="/static/home/lib/img/bank/Bank00.jpg"></a>'
                    );
                }

                $('.bank_list a').bind('click',ImgClickEvent);
            })
			var ImgClickEvent = function(event) {
				if(event.preventDefault){
				    event.preventDefault();
				}else{
				    event.returnValue = false;
				}
				if($(this).attr('id') == 'more_bank') {
					var more_bank = '<a id="g12" href="#"><img src="/static/home/lib/img/bank/Bank12.jpg"></a>';
					more_bank += '<a id="g33" href="#"><img src="/static/home/lib/img/bank/Bank13.jpg"></a>';
					more_bank += '<a id="g36" href="#"><img src="/static/home/lib/img/bank/Bank14.jpg"></a>';
					more_bank += '<a id="g55" href="#"><img src="/static/home/lib/img/bank/Bank15.jpg"></a>';
					more_bank += '<a id="g50" href="#"><img src="/static/home/lib/img/bank/Bank16.jpg"></a>';
					more_bank += '<a id="g15" href="#"><img src="/static/home/lib/img/bank/Bank17.jpg"></a>';
					more_bank += '<a id="g40" href="#"><img src="/static/home/lib/img/bank/Bank18.jpg"></a>';
					more_bank += '<a id="g49" href="#"><img src="/static/home/lib/img/bank/Bank19.jpg"></a>';
					more_bank += '<a id="g51" href="#"><img src="/static/home/lib/img/bank/Bank20.jpg"></a>';
					more_bank += '<a id="g53" href="#"><img src="/static/home/lib/img/bank/Bank21.jpg"></a>';
					more_bank += '<a id="g54" href="#"><img src="/static/home/lib/img/bank/Bank22.jpg"></a>';
					$(this).replaceWith(more_bank);
					$('.bank_list a').unbind('click');
					$('.bank_list a').bind('click',ImgClickEvent);
					$('#recharge_block').height($('#recharge_block').height() + 250);
					return;
				}
				//设定gateID
				me.gate_id = $(this).attr('id').substr(1);
				$('.bank_list a').css('borderColor','#CCC');
				$('.bank_list a').css('box-shadow','none');
				$(this).css('borderColor','#F93');
				$(this).css('box-shadow','0px 0px 2px #F93');
			};
			$('.bank_list a').bind('click',ImgClickEvent);
			$('#recharge_button').bind('click',function(event) {
				var buy_amount = $('#buy_amount').val();				
				if(!buy_amount) {
					// alert('请输入购买金额。');
					me.setAlertify('请输入购买金额.');
					return;
				}

                //by jiangshuguang
                if(buy_amount> P.data("limit_available")){
                    me.setAlertify('' +
                        '<div class="limit-available-tip">' +
                            '<div>' +
                                '<span>已超出您的购买额度,</span>' +
                                '<span>您当前可购买的额度为</span>' +
                            '</div>' +
                            '<div>' +
                                '￥'+ P.data('limit_available')+',' +
                                '<a href="http://mp.weixin.qq.com/s?__biz=MjM5MjQ2NjY1MA==&mid=200763590&idx=1&sn=a0a4356cb118ef6b7a96787db6b0e85f#rd">提高额度</a>'+
                            '</div>'+
                        '</div>');

                    return;
                }


				if(!me.gate_id) {
					me.setAlertify('请选择支付的银行。');
					return;
				}
				if(!$('#read_cb').is(':checked')) {
					me.setAlertify('请确认阅读自动交易与债券转让协议。');
					return;
				}

				if(!me.id_card) {
					if(!me.idverify) {
						var IDVerify = require('home:widget/ui/idverify/idverify.js');
						me.idverify = new IDVerify();
						$('#idverify_submit').bind('click',function() {
					        var opt = me.idverify.checkForm();
					        if(!opt) {
					        	return;
					        }
					        $.post('/account/setidcard',opt,function(data) {
					            if(data && data.error_no == 0) {
					            	alert('实名认证成功');
					            	me.id_card = opt.id_card;
					            	me.idverify.hide();
					            } else {
					            	me.idverify.setError(data.error_message);
					            }
					        },'json');
					    });
					}
					me.idverify.show();
					return;
				}
				window.open('/pay/createpayserial?amount=' + buy_amount + '&gate_id=' + me.gate_id);
				me.alertify.setTitle('支付提示');
				me.setAlertify('请在新的窗口中登录网上银行进行付款。在付款完成后可以在本页面上方查看我的交易记录查看充值结果，如果有问题可以稍后进行查看；如果长时间未收到本次充值的金额，请通过页面下方联系方式联系我们。');
			})
		},

		setAlertify: function(error_message) {
			var me = this;
			if(me.alertify) {
				me.alertify.setMessage(error_message);
			} else {
				var Alertify = require('home:widget/ui/alertify/alertify.js');
				me.alertify = new Alertify();
				me.alertify.setMessage(error_message);
			}
		},

		getPlatFund: function() {
			var me = this;
			$.getJSON('/plat/plat_fund_summary' ,function (data) {
				if(data && data.error_no == 0 && data.data) {
					var usable_credit = parseInt(data.data.plat_credit_buy);
					$('#usable_credit .ob_title').text('￥' + mathUtil.toCredit(usable_credit));
					$('#today_credit .ob_title').text('￥' + mathUtil.toCredit(data.data.plat_total_buy));
					var percent = mathUtil.toPercent(data.data.weight_profit_years_percent);
					$('.profit_percent').html(percent+'<span>%</span>');
                    usable_credit<=0 && $(".grid_right .no_usable_credit").show();
					me.renderPie(data.data);
				} else {
					if(data.error_no != 0) {
						alert(data.error_message);
					}
				}
			})
		},
		getValidateInfo : function() {
			var me = this;
			$.getJSON('/account/getuserstatus', function(data) {
				if(data && data.error_no == 0) {
					me.id_card = data.data.id_card;
					me.limit_buy = data.data.limit_buy;
				}
			});
		},
		renderPie : function(data) {
			$('.cycle').highcharts({
				chart : {
					backgroundColor:'rgba(0,0,0,0)',
					borderColor:"#AAA"
				},
				credits : {
					enabled:false
				},
				title: {
					userHTML : true,
					text: '  ',
					color:'#FFF',
					align: 'center',
					verticalAlign: 'middle',
					y: -10
				},
				tooltip: {
					pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
				},
				legend: {
					borderColor: '#909090'
				},
				plotOptions: {
					pie: {
						borderWidth:"0px",
						size:"100%",
						dataLabels: {
							enabled: true
						}
					}
				},

				series: [{
					type: 'pie',
					name: '占比',
					innerSize: '100%',
					data :[
						{
							name:'可购买份额',
							y:parseInt(data.plat_credit_buy),
							dataLabels : {
								enabled:false
							},
							color:'#336'
						},
						{
							name:'已购买份额',
							y:parseFloat(data.plat_total_buy) - parseInt(data.plat_credit_buy),
							dataLabels : {
								enabled:false
							},
							color:'#669'
						},
					]
				}]
			});
		}
	};
	rechargePage.init();
})();
